package com.hlkj.baojin.controller;

import com.hlkj.baojin.utils.hik.CfgMgr;
import com.hlkj.baojin.utils.hik.LogUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@RestController
@RequestMapping("/hkws")
public class ParamAction{
	
	private  String picType;
	private  String picPath;
	private  String videoSize;
	private  String videoPath;
	private  String decode; 
	private  String theme;
	private  String ocxType;
	
/*	private  String picTypePB;
	private  String picPathPB;
	private  String videoSizePB;
	private  String videoPathPB;
	private  String decodePB; */
	
	public String getOcxType() {
		return ocxType;
	}


	public void setOcxType(String ocxType) {
		this.ocxType = ocxType;
	}


	

	public String getTheme() {
		return theme;
	}


	public void setTheme(String theme) {
		this.theme = theme;
	}




	@Autowired
	private HttpServletRequest request;
	@Autowired
	private HttpServletResponse response;
	
	
	//获取全局参数
	@ApiOperation(value = "getParmAction", tags = {"测试"})
	@ApiResponses(value = {@ApiResponse(code = 200, message = "成功", response = void.class)})
	@PostMapping("/param/getParmAction")
	public void getParm(){
		response.setContentType("application/json;charset=UTF-8");
		JSONObject parmJson=new JSONObject();
		picType=(String) request.getSession().getAttribute("picType");
		picPath=(String) request.getSession().getAttribute("picPath");
		videoSize=(String) request.getSession().getAttribute("videoSize");
		videoPath=(String) request.getSession().getAttribute("videoPath");
		decode=(String) request.getSession().getAttribute("decode");
		if(picType!=null && !picType.equals("")){
			parmJson.put("picType", picType);
		}else{
			parmJson.put("picType", "jpeg");
		}
		if(picPath!=null && !picPath.equals("")){
			parmJson.put("picPath", picPath);
		}else{
			parmJson.put("picPath", "C:\\Users\\Public\\Pictures");
		}
		if(videoSize!=null && !videoSize.equals("")){
			parmJson.put("videoSize", videoSize);
		}else{
			parmJson.put("videoSize","256");
		}
		if(videoPath!=null && !videoPath.equals("")){
			parmJson.put("videoPath", videoPath);
		}else{
			parmJson.put("videoPath","C:\\Users\\Public\\Videos");
		}
		if(decode!=null && !decode.equals("")){
			parmJson.put("decode",decode);
		}else{
			parmJson.put("decode","5");
		}
		try {
			response.getWriter().print(parmJson);
		} catch (IOException e) {
			LogUtil.logError(getClass(), "response返回错误");
		}
	}
	

	//保存全局参数
	@ApiOperation(value = "saveParm", tags = {"测试"})
	@ApiResponses(value = {@ApiResponse(code = 200, message = "成功", response = void.class)})
	@PostMapping("/param/saveParm")
	public void saveParm(String picType, String picPath, String videoSize, String videoPath, String decode){
		response.setContentType("text/html;charset=UTF-8");
		JSONObject parmJson=new JSONObject();
		request.getSession().setAttribute("picType", picType);
		request.getSession().setAttribute("picPath", picPath);
		request.getSession().setAttribute("videoSize", videoSize);
		request.getSession().setAttribute("videoPath", videoPath);
		request.getSession().setAttribute("decode", decode);
		parmJson.put("result", "success");
		try {
			response.getWriter().print(parmJson.toString());
		} catch (IOException e) {
			LogUtil.logError(getClass(), "response返回错误");
		}
	}

	@ApiOperation(value = "getOcxSkinAction", tags = {"测试"})
	@ApiResponses(value = {@ApiResponse(code = 200, message = "成功", response = void.class)})
	@PostMapping("/param/getOcxSkinAction")
	public void getOcxSkin(){
		response.setContentType("text/xml;charset=UTF-8");
		PrintWriter pw = null;
		String value = "";
//		Properties properties = new Properties();  
//		InputStream inputStream = null;
//		try {
//			inputStream = ParamAction.class.getClassLoader().getResourceAsStream("system.properties");
//			properties.load(inputStream);  
//		}catch(IOException e){
//			e.printStackTrace();  
//		}finally {
//			if(inputStream != null) {
//				try {
//					inputStream.close(); //关闭流   
//				}catch (IOException e) {  
//					e.printStackTrace();  
//				}
//			}
//		}
		try{
			if("previewOcx".equals(ocxType)){
			 value = CfgMgr.getPreviewOcxTheme();
			}else if("playbackOcx".equals(ocxType)){
		     value = CfgMgr.getPlaybackOcxTheme();
			}
			pw = response.getWriter();
			pw.print(value);
		}catch (Exception e) {
			LogUtil.logError(this, "无法获取主题信息");
		} finally {
			if (null != pw) {
				pw.close();// 关闭流
			}
		}
	}
	
	public void setOcxSkin(){
		response.setContentType("text/xml;charset=UTF-8");
		PrintWriter pw = null;
		try{
		   if("previewOcx".equals(ocxType)){
		       CfgMgr.setPreviewOcxTheme(theme);;
		   }else if("playbackOcx".equals(ocxType)){
			   CfgMgr.setPlaybackOcxTheme(theme);;
		   }
		   pw = response.getWriter();
		   pw.print("0");
		}catch (Exception e) {
			LogUtil.logError(this, "无法获取主题信息");
		} finally {
			if (null != pw) {
				pw.close();// 关闭流
			}
		}
		
//		Properties properties = new Properties();  
//		InputStream inputStream = null;
//		try {
//			inputStream = ParamAction.class.getClassLoader().getResourceAsStream("system.properties");
//			properties.load(inputStream); 
//			properties.put("ocx.skin", skin);			
//		}catch(IOException e){
//			e.printStackTrace();  
//		}finally {
//			if(inputStream != null) {
//				try {
//					inputStream.close(); //关闭流   
//				}catch (IOException e) {  
//					e.printStackTrace();  
//				}
//			}
//		}
		
	}

	
	public String getPicType() {
		return picType;
	}
	public void setPicType(String picType) {
		this.picType = picType;
	}
	public String getPicPath() {
		return picPath;
	}
	public void setPicPath(String picPath) {
		this.picPath = picPath;
	}
	public String getVideoSize() {
		return videoSize;
	}
	public void setVideoSize(String videoSize) {
		this.videoSize = videoSize;
	}
	public String getVideoPath() {
		return videoPath;
	}
	public void setVideoPath(String videoPath) {
		this.videoPath = videoPath;
	}
	public String getDecode() {
		return decode;
	}
	public void setDecode(String decode) {
		this.decode = decode;
	}
	public void setServletRequest(HttpServletRequest request) {
		this.request=request;
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response=response;
	}


}
